<?php
/**
 * Description: 获取学习历史记录
 * User: devforma
 * Date: 15/4/9
 * Time: 16:38
 */

require __DIR__ . '/includes/utils.php';
class GethistoryPapers implements iController {
    private $paperTypeRel;

    function __construct() {
        $this->paperTypeRel = array(
            '天天模考' => 'mokao',
            '专项练习' => 'note',
            '快速练习' => 'auto',
            '整卷练习' => 'entire',
            '错题练习' => 'error',
            '收藏练习' => 'collect',
            '估分' => 'evaluate',
            '模考' => 'mock'
        );

    }

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;
        
        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        if (!isset($queryArgs['offset']) || !is_numeric($queryArgs['offset'])
            || !isset($queryArgs['count']) || !is_numeric($queryArgs['count']) || $queryArgs['count'] <= 0) {
            return genOutput(1000);
        }

        $userId = $queryArgs['user_id'];

        //获取该用户的历史学习记录，
        //status字段为0表示创建了但没有做的试卷，为1表示完成的试卷，为2表示做了但未完成的试卷
        $sql = 'SELECT `id`, `paper_type`, `paper_title`, `action_time`, `status`, `right_count`, `total_count`, `extra_info`
        FROM `qb_relationship_user_paper` WHERE `user_id`=:user_id AND `status`!=0 ORDER BY `action_time` DESC LIMIT '.$queryArgs['offset']
        .', '.$queryArgs['count'];
        $papers = $cmsdb->getResults($sql, array(':user_id' => $userId));

        $list = array();
        foreach ($papers as $p) {
            $accuracy = $p->total_count == 0 ? 0.00 : round($p->right_count/$p->total_count, 2); //计算试卷正确率
            $paperType = getPaperType($p->paper_type)->type_name;

            $list[] = array(
                'paper_id' => intval($p->id),
                'paper_type' => $this->paperTypeRel[$paperType],
                'name' => $p->paper_title,
                'accuracy' => $accuracy,
                'status' => $p->status == 2 ? 'undone' : 'done',
                'action_time' => $p->action_time,
                'hierarchy_level' => 0,
                'hierarchy_id' => 0
            );
        }

        return genOutput(1, array(
            'list' => $list
        ));
    }
}